package com.xu.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author: 生如夏花
 * @Date: 2022/3/15 09:49
 */
@Component
public class SpringRabbitListener {

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String msg){
        System.out.println ("接收到消息"+msg);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenWorkQueue1(String msg){
        System.out.println ("接收到消息消费者1："+msg);
    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenWorkQueue2(String msg){
        System.out.println ("接收到消息消费者2："+msg);
    }

    @RabbitListener(bindings = @QueueBinding (
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "test.direct",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void listenDirectQueue1(String msg){
        System.out.println ("接收到消息:"+msg);
    }

    @RabbitListener(bindings = @QueueBinding (
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "test.direct"),
            key = {"red","yellow"}
    ))
    public void listDirectQueue2(String msg){
        System.out.println ("接收到消息："+msg);
    }

    @RabbitListener(bindings = @QueueBinding (
            value = @Queue("topic.queue1"),
            exchange = @Exchange(value = "test.topic",type = ExchangeTypes.TOPIC),
            key = {"#.news"}
    ))
    public void listTopicQueue1(String msg){
        System.out.println ("接收到消息："+msg);
    }

//    @RabbitListener(bindings = @QueueBinding (value = @Queue("object.queue"),exchange = @Exchange("object.exchange")))
    @RabbitListener(queues = "object.queue")
    public void listObjectQueue(Map<String, Object> map){
        System.out.println ("接收到消息"+map);
    }

}
